﻿using System;
using System.Text;
using System.Collections.Generic;

namespace T.FAS.Runtime.DataAccess.SQLDom
{
    /// <summary>
    /// Select语句分组元素
    /// </summary>
    /// <remarks>类的补充说明</remarks>
    [Serializable]
    public class GroupByStatement : SqlElement
    {
        #region 常量

        /// <summary> 
        /// SelectListStatement 
        /// </summary>
        public const string SELECTLISTSTATEMENT = "GroupByStatement";

        #endregion       

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public GroupByStatement()
        {
            base.CreateChildCollection();
        }

        #endregion       

        #region 方法

        /// <summary>
        /// 转换成SQL
        /// </summary>
        /// <returns>SQL</returns>
        public override string ToSQL()
        {
            StringBuilder result = new StringBuilder("");
            for (int index = 0; index < this.ChildCollection.Count; index++)
            {
                result.Append(this.ChildCollection[index].ToSQL());
                if (index < this.ChildCollection.Count - 1)
                    result.Append(",");
            }

            return result.ToString();
        }

        public string ToSQL_GroupBy()
        {
            StringBuilder result = new StringBuilder("");
            for (int index = 0; index < this.ChildCollection.Count; index++)
            {
                result.Append((this.ChildCollection[index] as GroupByField).ToSQL_GroupBy());
                if (index < this.ChildCollection.Count - 1)
                    result.Append(",");
            }

            return result.ToString();
        }

        #endregion
    }

    /// <summary>
    /// Select语句分组元素
    /// </summary>
    /// <remarks>类的补充说明</remarks>
    [Serializable]
    public class GroupByField : Field
    {
        #region 常量

        /// <summary> 
        /// SelectListStatement 
        /// </summary>
        public const string SELECTLISTSTATEMENT = "GroupByField";

        #endregion       

        #region 构造函数

        /// <summary>
        /// 构造函数
        /// </summary>
        public GroupByField()
        {

        }

        #endregion

        #region Property
        private string alias = string.Empty;

        /// <summary>
        /// 如果分组列展示在select查询结果中时应用的别名
        /// </summary>
        public string Alias
        {
            get
            {
                if (string.IsNullOrEmpty(alias))
                    return $"group_{Table.RealTableName}_{FieldName}";
                else
                    return alias;

            }
            set
            {
                alias = value;
            }
        }

        /// <summary>
        /// CompositeNodeObject.Element.ID
        /// </summary>
        public string CompositeNodeObjectElementID { get; set; }
        #endregion

        #region 方法

        /// <summary>
        /// 转换成SQL
        /// </summary>
        /// <returns>SQL</returns>
        public override string ToSQL()
        {
            return $"{Table.TableAlias}.{FieldName}";
        }

        public string ToSQL_GroupBy()
        {
            return $"{Table.TableAlias}.{FieldName}";
        }
        #endregion
    }
}
